<?php
/* Modified straight directory media handler */

class media
{	
	public $filename, $filetype, $filesize, $last_modified_date;
	
	private  $filepath;
	
	function __construct($var)
	{	
		if(is_array($var))
			$this->upload($var);
		else
			$this->load($var);
	}
	
	public final static function get_settings()
	{
		return (object)array(
			'directory' => DIR_ROOT . '/uploads/media',
			'url' => WWW_ROOT . 'uploads/media',
		);
	}
	
	private function upload($file)
	{
		$this->filename = $tmp_filename = $file["name"];
    mkdirs( $this->get_settings()->directory);
    
		$i = 0;
		while(file_exists($this->get_settings()->directory . "/" . $tmp_filename))
		{
			$i++;
			$tmp_filename = $this->get_filename() . '(' . $i . ')' . $this->get_extension();
		}
		
		$this->filepath = $this->get_settings()->directory . "/" . ($this->filename = $tmp_filename);
		
		move_uploaded_file($file["tmp_name"], $this->filepath);
	}
	
	private function load($filename)
	{
		if(file_exists($this->get_settings()->directory . "/" . basename($filename)))
		{
			$this->filename = basename($filename);
			$this->filepath = $this->get_settings()->directory . "/" . $this->filename;
			$this->filetype = $this->get_filetype();
			$this->filesize = filesize($this->filepath);
			$this->last_modified_date = filemtime($this->filepath);
		}
		else
			throw new Exception('Media not found.');		
	}
	
	public function delete()
	{
		if(file_exists($this->filepath))
		{
			if(unlink($this->filepath))
				return true;
		}
		
		return false;
	}
	
	public function get_url()
	{
		return $this->get_settings()->url . "/" . $this->filename;
	}
	
	private function get_extension() 
    { 		
		$pieces = explode('.', $this->filename);
		return '.' . $pieces[count($pieces) - 1];
    } 
    
    private function get_filename() 
    { 
		$pieces = explode('.', $this->filename);
		unset($pieces[count($pieces) - 1]);
		return implode('.', $pieces);
    } 
	
	private function get_filetype()
	{
		$ext_types['.flv']  = 'video';
	    $ext_types['.pdf']  = 'pdf';
	    $ext_types['.doc']  = 'word';
	    $ext_types['.png']  = 'img';
	    $ext_types['.jpg']  = 'img';
	    $ext_types['.jpeg'] = 'img';
	    $ext_types['.gif']  = 'img';
	    $ext_types['.zip']  = 'zip';  
	  
	    $ext = strtolower($this->get_extension());
	    $type = $ext_types[$ext];
	    if($type=='') $type = 'file';
		
	    return $type;
	}
  
  	// Sorting functions ////////////////////////////////////
	public static function sort_by_filename_asc($a, $b)
	{
		if($a->filename == $b->filename)
			return 0;
		
		return ($a->filename > $b->filename) ? +1 : -1;
	}	
	public static function sort_by_filename_desc($a, $b)
	{
		if($a->filename == $b->filename)
			return 0;
		
		return ($a->filename < $b->filename) ? +1 : -1;
	}
	public static function sort_by_filetype_asc($a, $b)
	{
		if($a->filetype == $b->filetype)
			return 0;
		
		return ($a->filetype > $b->filetype) ? +1 : -1;
	}	
	public static function sort_by_filetype_desc($a, $b)
	{
		if($a->filetype == $b->filetype)
			return 0;
		
		return ($a->filetype < $b->filetype) ? +1 : -1;
	}
	public static function sort_by_filesize_asc($a, $b)
	{
		if($a->filesize == $b->filesize)
			return 0;
		
		return ($a->filesize > $b->filesize) ? +1 : -1;
	}	
	public static function sort_by_filesize_desc($a, $b)
	{
		if($a->filesize == $b->filesize)
			return 0;
		
		return ($a->filesize < $b->filesize) ? +1 : -1;
	}
	public static function sort_by_last_modified_date_asc($a, $b)
	{
		if($a->last_modified_date == $b->last_modified_date)
			return 0;
		
		return ($a->last_modified_date > $b->last_modified_date) ? +1 : -1;
	}	
	public static function sort_by_last_modified_date_desc($a, $b)
	{
		if($a->last_modified_date == $b->last_modified_date)
			return 0;
		
		return ($a->last_modified_date < $b->last_modified_date) ? +1 : -1;
	}
	/////////////////////////////////////////////////////////
}

?>